<template>
	<view class="container">
		<view class="left-bottom-sign"></view>
		<!-- <view class="back-btn yticon icon-zuojiantou-up" @click="navBack"></view>
		 -->
		<view class="right-top-sign"></view>
		<!-- 设置白色背景防止软键盘把下部绝对定位元素顶上来盖住输入框等 -->
		<view class="wrapper">
			<view class="left-top-sign">LOGIN</view>
			<view class="welcome">
				农业农村局监理平台
			</view>
			<view>
				<view class="input-content">
					<view class="input-item">
						<text class="tit">手机号码</text>
						<input type="number" :value="phone" placeholder="请输入手机号码" maxlength="11" data-key="phone"
							@input="inputChange" />
					</view>
					<view class="input-item">
						<text class="tit">密码</text>
						<input type="password" :value="password" data-key="password" placeholder="请输入密码"
							@input="inputChange" />
					</view>
					<!-- <view class="input-item">
						<text class="tit">验证码</text>
						<view class="verify-code">
							<input type="mobile" value="" placeholder="6位验证码" maxlength="6" data-key="verifyCode"
								@input="inputChange" style="width: 60%;" />
							<button style="width: 40%;" :disabled="sendDisabled"
								@click="doGetVerify">{{sendText}}</button>
						</view>
					</view> -->
				</view>
				<button class="confirm-btn" @click="toLogin" :disabled="logining">登录</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phone: '',
				password: '',
				logining: false,
				sendText: '获取验证码',
				sendDisabled: false,
				params: "",
				url: 'pages/index/index'
			}
		},
		onShow() {
			// this.$api.logout();
		},
		onLoad(options) {
			let prePage = "";
			if (options.state) {
				this.url = options.state;
			} else {
				let pages = getCurrentPages();
				prePage = pages[pages.length - 3];
				if (prePage) {
					this.url = prePage.route;
				} else {
					this.url = 'pages/index/index'
				}
			}
			// this.getCode(this.url);
		},
		methods: {
			navBack() {
				uni.navigateBack();
			},
			getUrlCode() {
				// 截取url中的code方法
				var url = location.search;
				var theRequest = new Object();
				if (url.indexOf("?") != -1) {
					var str = url.substr(1);
					var strs = str.split("&");
					for (var i = 0; i < strs.length; i++) {
						theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
					};
				};
				// console.log(theRequest);
				return theRequest;
			},

			inputChange(e) {
				const key = e.currentTarget.dataset.key;
				this[key] = e.detail.value;
			},

			async toLogin() {
				const that = this;
				if (that.phone.length !== 11) {
					that.$api.msg('请输入11位手机号');
				}
				/* else if (!that.verifyCode) {
									that.$api.msg('请输入验证码');
								} */
				else {
					that.logining = true;
					//若是App登录，则不需要保存OpenId。可直接登录
					that.$api.request('admin', 'loginByFront', {
						username: that.phone,
						password: that.password
					}, failres => {
						uni.showModal({
							content: failres.errmsg
						})
						that.logining = false;
					}).then(res => {
						that.$api.logout()
						that.$api.globalData = {};
                        that.logining = true;
						that.$store.commit('login', res.data);
						uni.setStorageSync('userInfo', res.data);

						/* uni.showModal({
							content: this.url+(typeof this.url)
						}) */
						let stateinfo = (this.url && this.url != 'undefined') ? this.url : 'pages/index/index';
						if (stateinfo === 'pages/article/list' || stateinfo === 'pages/user/user' ||
							stateinfo === 'pages/index/index' || stateinfo === 'pages/category/category') {
							uni.redirectTo({
								url: '/' + stateinfo
							})

							// uni.switchTab({
							// 	url: '/' + stateinfo,
							// 	success: (res) => {
							// 		let page = getCurrentPages().pop();
							// 		if (page == undefined || page == null) return;
							// 		page.onLoad();
							// 	}
							// })
						} else if (stateinfo === 'subPackages/subPackageA/pages/product/detail') {
							uni.redirectTo({
								url: "/pages/index/index"
							})
						} else if (stateinfo === 'pages/public/login') {
							uni.redirectTo({
								url: "/pages/storemanage/storemanage"
							})
						} else {
							uni.redirectTo({
								url: stateinfo
							})
						}
					});

				};
			}
		}
	}
</script>

<style lang='scss'>
	page {
		background: #fff;
	}

	.container {
		padding-top: 115px;
		position: relative;
		width: 100vw;
		height: 100vh;
		overflow: hidden;
		background: #fff;
	}

	.left-bottom-sign {
		position: absolute;
		left: -270upx;
		bottom: -320upx;
		border: 100upx solid #d0d1fd;
		border-radius: 50%;
		padding: 180upx;
	}

	.back-btn {
		position: absolute;
		left: 40upx;
		z-index: 9999;
		padding-top: var(--status-bar-height);
		top: 40upx;
		font-size: 40upx;
		color: $font-color-dark;
	}

	.right-top-sign {
		position: absolute;
		top: 80upx;
		right: -30upx;
		z-index: 95;

		&:before,
		&:after {
			display: block;
			content: "";
			width: 400upx;
			height: 80upx;
			background: #b4f3e2;
		}

		&:before {
			transform: rotate(50deg);
			border-radius: 0 50px 0 0;
		}

		&:after {
			position: absolute;
			right: -198upx;
			top: 0;
			transform: rotate(-50deg);
			border-radius: 50px 0 0 0;
		}
	}

	.wrapper {
		position: relative;
		z-index: 90;
		background: #fff;
		padding-bottom: 40upx;

		.left-top-sign {
			font-size: 120upx;
			color: $page-color-base;
			position: relative;
			left: -16upx;
		}

		.welcome {
			position: relative;
			text-align: center;
			top: -90upx;
			font-size: 46upx;
			color: #555;
			text-shadow: 1px 0px 1px rgba(0, 0, 0, .3);
		}

		.input-content {
			padding: 0 60upx;

			.input-item {
				display: flex;
				flex-direction: column;
				align-items: flex-start;
				justify-content: center;
				padding: 0 30upx;
				background: $page-color-light;
				height: 120upx;
				border-radius: 4px;
				margin-bottom: 50upx;

				&:last-child {
					margin-bottom: 0;
				}

				.tit {
					height: 50upx;
					line-height: 56upx;
					font-size: $font-sm+2upx;
					color: $font-color-base;
				}

				input {
					height: 60upx;
					font-size: $font-sm + 2upx;
					color: $font-color-dark;
					width: 100%;
				}

				.verify-code {
					flex-direction: row;
					display: flex;
					justify-content: end;
					flex-wrap: wrap
				}
			}
		}

		.confirm-btn {
			width: 630upx;
			height: 76upx;
			line-height: 76upx;
			border-radius: 50px;
			margin: 70upx auto;
			background: $a-color;
			color: #fff;
			font-size: $font-lg;

			&:after {
				border-radius: 100px;
			}
		}
	}
</style>